package com.coolwen.service;

import com.coolwen.dao.CategoryMapper;
import com.coolwen.dao.ProductMapper;
import com.coolwen.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2016/7/14.
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper pdtDao;

    @Autowired
    private CategoryMapper categoryDao;

    @Override
    public void add(int cid, Product product) {
        Category c = categoryDao.selectByPrimaryKey(cid);
        if (c == null) throw new ShopException("要添加产品的类别不存在!");
        product.setcId(c.getId());
        pdtDao.insert(product);
    }

    @Override
    public void update(int cid, Product product) {
        Category c = categoryDao.selectByPrimaryKey(cid);
        if (c == null) throw new ShopException("要更新的产品的类别不存在!");
        product.setcId(c.getId());
        pdtDao.update(product);

    }

    @Override
    public void delete(int id) {
        // TODO 如果用户购买了该商品就不能删除，该商品存在订单也不能删除，
        //如果可以删除商品的话需要删除商品的图片
        Product p = this.load(id);
        String img = p.getImg();
//        super.delete(Product.class, id);
        //删除图片
        String path = SystemContext.getRealpath() + "/resources/upload/";
        System.out.println("图片的路径:" + path + img);
        File f = new File(path + img);
        f.delete();
        pdtDao.deleteByPrimaryKey(id);
    }

    @Override
    public Product load(int id) {

        return pdtDao.load(id);
    }


    /**
     * 可以通过商品类别和名称进行搜索
     * 此时可以进行灵活的排序
     *
     * @param cid
     * @param name
     * @param status
     * @return
     */
    @Override
    public Pager<Product> find(int cid, String name, int status) {
        int pageSize = SystemContext.getPageSize();
        int pageOffset = SystemContext.getPageOffset();
//        SystemContext.getOrder();
//        SystemContext.getPageIndex();
//        SystemContext.getSort();
        Pager<Product> pages = new Pager<Product>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageSize", pageSize);
        params.put("pageOffset", pageOffset);
        if (cid > 0) {
            params.put("cid", cid);
        }
        if (name != null && !"".equals(name.trim())) {
            params.put("name", "%" + name + "%");
        }
        if (status == 1 || status == -1) {
            params.put("status", status);
        }
        List<Product> datas = pdtDao.find(params);
        int totalRecord = pdtDao.find_count(params);
        pages.setDatas(datas);
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);
        return pages;
    }

    /**
     * 增加库存
     *
     * @param id
     * @param num
     */
    @Override
    public void addStock(int id, int num) {

    }

    /**
     * 减少库存
     *
     * @param id
     * @param num
     */
    @Override
    public void decreaseStock(int id, int num) {

    }

    /**
     * 变更状态
     *
     * @param id
     */
    @Override
    public void changeStatus(int id) {

    }

}
